package net.mikoo.seals.biz.manager;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.mikoo.seals.biz.mapper.NoticeMapper;
import net.mikoo.seals.biz.mapper.PersonMapper;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.common.enums.NoticeStatusEnum;
import net.mikoo.seals.common.enums.NoticeTypeEnum;
import net.mikoo.seals.common.enums.VerifyStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.Notice;
import net.mikoo.seals.model.Person;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.utils.DateUtil;

@Component
public class NoticeManager {
	
	@Autowired
	private PersonMapper personMapper;

	@Autowired
	private NoticeMapper noticeMapper;
	
	@Autowired
	private WechatManager wechatManager;
	
	
	public void apply(Apply apply,Seal seal,ApplyVerify verify) {
		/***
		 * title:张三申请财务印章使用需要您审核
		 * 申请印章：
		 * 申请类型
		 * 申请时间:
		 * 申请次数：
		 * 申请事由:
		 * remark
		 */
		
		String sealName = seal.getSealName();
		String applType = "";
		
		Person person = personMapper.selectByPartyId(apply.getPartyId());
		
		if(apply.getApplyType().equals(ApplyTypeEnum.USED_APPLY.code())) {
			applType = "使用";
		}else if(apply.getApplyType().equals(ApplyTypeEnum.TAKE_APPLY.code())){
			applType = "外带";
		}
		
		String title= person.getName().concat("申请").concat(sealName).concat(applType).concat("需要您审批");
		
		JSONObject content = new JSONObject();
		content.put("title", title);
		content.put("keyword1", sealName);
		content.put("keyword2", ApplyTypeEnum.getByCode(apply.getApplyType()).getMessage());
		content.put("keyword3", DateUtil.formate(apply.getApplyTime(), "yyyy-MM-dd HH:mm:ss"));
		content.put("keyword4", apply.getApplyTimes()+"次");
		content.put("keyword5", apply.getCause());
		content.put("remark", "");
		
		add(verify.getCompanyId(), verify.getPartyId(), verify.getApplyVerifyId(), NoticeTypeEnum.APPLY.code(), title, content.toString());
		
		wechatManager.await(apply, seal, verify);
	}
	
	
	public void verify(Apply apply,Seal seal,ApplyVerify verify) {
		
		/***
		 * title:您申请财务印章使用已同意，请知晓
		 * 申请印章：
		 * 申请类型
		 * 申请时间:
		 * 审核人：
		 * 拒绝原因:
		 * remark
		 */
		
		
		String sealName = seal.getSealName();
		String applType = "";
		
		Person person = personMapper.selectByPartyId(verify.getPartyId());
		
		if(apply.getApplyType().equals(ApplyTypeEnum.USED_APPLY.code())) {
			applType = "使用";
		}else if(apply.getApplyType().equals(ApplyTypeEnum.TAKE_APPLY.code())){
			applType = "外带";
		}
		
		String result = VerifyStatusEnum.getByCode(verify.getVerifyStatus()).getMessage();
		
		
		String title= "您申请的".concat(sealName).concat(applType).concat(result).concat("，请知晓");
		
		JSONObject content = new JSONObject();
		content.put("title", title);
		content.put("keyword1", sealName);
		content.put("keyword2", ApplyTypeEnum.getByCode(apply.getApplyType()).getMessage());
		content.put("keyword3", DateUtil.formate(apply.getApplyTime(), "yyyy-MM-dd HH:mm:ss"));
		content.put("keyword4", person.getName());
		content.put("keyword5", verify.getVerifyResult());
		content.put("remark", result);
		
		add(verify.getCompanyId(), apply.getPartyId(), apply.getApplyId(), NoticeTypeEnum.VERIFY.code(), title, content.toString());
		
		System.out.println(verify.getVerifyStatus());
		
		if(verify.getVerifyStatus().equals(VerifyStatusEnum.PASSED.code())) {
			wechatManager.pass(apply, seal, verify);
		}else if(verify.getVerifyStatus().equals(VerifyStatusEnum.REJECT.code())) {
			wechatManager.reject(apply, seal, verify);
		}
	}
	

	private void add(Long companyId, Long partyId, Long objectId, String noticeType, String title, String content) {

		if (companyId == null) {
			throw new ParametersException("companyId");
		}
		if (partyId == null) {
			throw new ParametersException("partyId");
		}
		if (objectId == null) {
			throw new ParametersException("objectId");
		}
		if (StringUtils.isBlank(noticeType)) {
			throw new ParametersException("type");
		}
		if (StringUtils.isBlank(title)) {
			throw new ParametersException("title is isBlank");
		}

		Notice notice = new Notice();
		notice.setTitle(title);
		notice.setContent(content);
		notice.setNoticeType(noticeType);
		notice.setPartyId(partyId);
		notice.setCompanyId(companyId);
		notice.setObjectId(objectId);
		notice.setNoticeStatus(NoticeStatusEnum.UNREAD.code());
		notice.setCreateTime(new Date());
		noticeMapper.insert(notice);

	}

}
